#include "VTitleBarWidget.h"
#include "ui_VTitleBarWidget.h"
#include "VGlobal.h"

#include <QDockWidget>
#include <QMenu>
#include <QAction>
#include <QToolBar>

VTitleBarWidget::VTitleBarWidget(QWidget *parent) :
    QFrame(parent),
    ui(new Ui::VTitleBarWidget),
    _action_width(this),
    _action_height(this)
{
    ui->setupUi(this);
    this->setObjectName("title_bar_widget");
    ui->label->setObjectName("title_bar_widget_label");
    ui->btn_close->setObjectName("title_bar_widget_button_close");
    ui->btn_normal->setObjectName("title_bar_widget_button_normal");
    ui->btn_expand->setObjectName("title_bar_widget_button_expand");

//    ui->btn_close->setIcon(QIcon(VGlobal::icon_path.value("titlebar_close_icon")));
//    ui->btn_normal->setIcon(QIcon(VGlobal::icon_path.value("titlebar_normal_icon")));
//    ui->btn_expand->setIcon(QIcon(VGlobal::icon_path.value("dock_expand")));

    _action_width.setText("宽度固定");
    _action_width.setCheckable(true);
    _action_height.setText("高度固定");
    _action_height.setCheckable(true);

    _menu.addAction(&_action_width);
    _menu.addAction(&_action_height);

//    ui->widget->addAction("sdfds");
//    auto p = new QComboBox();
//    p->setMinimumHeight(23);
//    ui->widget->addWidget(p);
////    ui->widget->setContentsMargins(0, 0, 0, 0);
//    QAction *a = new QAction(this);
//    a->setIcon(QIcon(VGlobal::icon_path.value("search")));
//    a->setCheckable(true);
//    ui->widget->addAction(a);
//    ui->widget->setIconSize(QSize(15, 15));
//    ui->widget->setContentsMargins(0, 0, 0, 0);
//    ui->widget->layout()->setMargin(0);
//    ui->widget->layout()->setSpacing(0);
//    auto c = new QComboBox();
//    c->setEditable(true);
//    ui->widget->addWidget(c);
////    QToolBar *toolbar;
////    toolbar = new QToolBar(ui->widget);
////    toolbar->addAction("sfds");
}

VTitleBarWidget::~VTitleBarWidget()
{
    delete ui;
}

QSize VTitleBarWidget::minimumSizeHint() const
{
    QDockWidget *dw = qobject_cast<QDockWidget*>(parentWidget());
    Q_ASSERT(dw != 0);
    QSize result(200, 25);
    if (dw->features() & QDockWidget::DockWidgetVerticalTitleBar)
        result.transpose();

    ui->label->setText(parentWidget()->windowTitle());
    connect(dw, &QDockWidget::windowTitleChanged, [=](const QString &title){
        ui->label->setText(title);
    });

    return result;
}

VToolBar *VTitleBarWidget::get_toolbar()
{
    return ui->title_toolbar;
}


void VTitleBarWidget::on_btn_normal_clicked()
{
    QDockWidget *dw = qobject_cast<QDockWidget*>(parentWidget());
    dw->setFloating(!dw->isFloating());
}

void VTitleBarWidget::on_btn_close_clicked()
{
    emit signal_close_clicked();
}

void VTitleBarWidget::on_btn_expand_clicked()
{
    QPoint pos = ui->btn_expand->pos();
    pos.setY(pos.y() + ui->btn_expand->height());
    _menu.exec(this->mapToGlobal(pos));
    emit signal_dock_setting(_action_width.isChecked(),
                             _action_height.isChecked());
}
